#include "mainwindow.h"
#include "ui_mainwindow.h"

#include "zreader.h"
#include "CardRW.h"
#include "FarmerCard.h"
#include "CardReader.h"
#include <QMessageBox>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    uiCardRW = new CardRW(ui->gbYeMian); //卷烟卡界面
    uiFam = new FarmerCard(ui->gbYeMian); //烟农卡界面

    connect(uiCardRW, SIGNAL(pbEvent(int)), this, SLOT(onUiEvent(int)));
    connect(uiFam, SIGNAL(pbEvent(int)), this, SLOT(onUiEvent(int)));

    uiCardRW->show();
    uiFam->hide();

    creader = new CardReader(NULL, 1); //读卡器
    connect(creader, SIGNAL(sendMsg(int, QByteArray)), this, SLOT(recvMsg(int, QByteArray)));

    //是否读过卡
    yl_isread = false;
    jy_isread = false;
}

MainWindow::~MainWindow()
{
    delete ui;
}

/*
 * 接受数据
*/
void MainWindow::recvMsg(int mode, QByteArray data)
{
    switch (mode)
    {
    case 0: //显示错误信息
    {
        QString str;

        str = data;
        statusBar()->showMessage(str, -1);
    }break;
    case 1: //显示读烟农卡的数据
    {
        uiFam->ShowDuKa(data);
        yl_isread = true;
    }break;
    case 2: //显示读卷烟卡的数据
    {
        uiCardRW->ShowDuKa(data);
        jy_isread = true;
    }break;
    }
}

/*
 接受界面上的按钮消息
*/
void MainWindow::onUiEvent(int e)
{
    QByteArray data;

    switch (e)
    {
    case 1:
    {
    }break;
    case 2:
    {
        jy_isread = false;
        emit creader->readReq(2);
    }break;
    case 3:
    {
        QByteArray key;
        QByteArray blk3;
        int mode;

        if (!jy_isread)
        {
            showMsg("请先读取卡片信息");
            break;
        }

        if (uiCardRW->GetInfo(data, key, blk3, mode))
        {
            int opt;

            opt = QMessageBox::question(NULL, "确认", "是否写入", QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
            if (opt == QMessageBox::Yes)
                emit creader->writeReq(data, key, blk3, mode);
        }
        else
        {
            showMsg(uiCardRW->statusTip(), 3000);
        }
    }break;
    case 10:
    {
        yl_isread = false;
        emit creader->readReq(1);
    }break;
    case 11:
    {
        QByteArray key;
        QByteArray blk3;
        int mode;

        if (uiFam->GetInfo(data, key, blk3, mode))
        {
            int opt;

            opt = QMessageBox::question(NULL, "确认", "是否写入", QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
            if (opt == QMessageBox::Yes)
                emit creader->writeReq(data, key, blk3, mode);
        }
        else
        {
            showMsg(uiFam->statusTip(), 3000);
        }
    }break;
    }
}

void MainWindow::showMsg(QString str, int t)
{
    statusBar()->showMessage(str, t);
}

void MainWindow::on_btFarmerCard_clicked()
{
    uiCardRW->hide();
    uiFam->show();
}

void MainWindow::on_btJuanYanKa_clicked()
{
    uiCardRW->show();
    uiFam->hide();
}

void MainWindow::on_btTuiKa_clicked()
{
    uiCardRW->Clear();
    uiFam->Clear();
    yl_isread = false;
    jy_isread = false;
    emit creader->readReq(3);
}
